package com.ouyy.volunteerservice.volunteer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ouyy.volunteerservice.volunteer.domain.po.Medieaview;
import com.ouyy.volunteerservice.volunteer.domain.po.volunteerView;

import com.ouyy.volunteerservice.volunteer.service.volunteerViewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/volunteerview")
public class volunteerViewController {

    @Autowired
    private volunteerViewService volunteerViewService;

    // 查询所有风采
    @GetMapping("/all")
//    @Authowired
    public ResponseEntity<List<volunteerView>> getAllvolunteerViews() {
        List<volunteerView> list = volunteerViewService.list();
        return ResponseEntity.ok(list);
    }

    @PostMapping("/search")
    public ResponseEntity<List<volunteerView>> searchByTitle(@RequestBody Map<String,String> titleMap) {
        String title = titleMap.get("title");
        QueryWrapper<volunteerView> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        List<volunteerView> results = volunteerViewService.list(queryWrapper);

        if (!results.isEmpty()) {
            return ResponseEntity.ok(results);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 根据 ID 查询公告
    @GetMapping("/{id}")
    public ResponseEntity<volunteerView> getvolunteerViewById(@PathVariable("id") Integer id) {
        //在点击详情前新增点击数
        volunteerViewService.incrementClickCount(id);
        volunteerView volunteerView = volunteerViewService.getById(id);
        if (volunteerView != null) {
            return ResponseEntity.ok(volunteerView);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 新增公告
    @PostMapping("/")
    public ResponseEntity<volunteerView> createvolunteerView(@RequestBody volunteerView volunteerViewPO) {
        boolean saved = volunteerViewService.save(volunteerViewPO);
        if (saved) {
            return ResponseEntity.ok(volunteerViewPO);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // 修改公告
    @PutMapping("/{id}")
    public ResponseEntity<volunteerView> updatevolunteerView(@PathVariable("id") Integer id, @RequestBody volunteerView volunteerViewPO) {
        volunteerViewPO.setId(id);
        System.out.println(volunteerViewPO.getImageUrl());
        boolean updated = volunteerViewService.updateById(volunteerViewPO);
        if (updated) {
            return ResponseEntity.ok(volunteerViewPO);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 删除公告
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletevolunteerView(@PathVariable("id") Integer id) {
        boolean deleted = volunteerViewService.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
